package com.rd.student.jike.class4.xiayinyin.week04.day26;

import java.io.File;
import java.io.IOException;

public class Demo01 {
    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);

        String filePath = projectPath+ "/" + "test.txt";
        String fileOath1 = projectPath + File.separator + "test.txt";
        System.out.println(fileOath1);
        File file = new File(filePath);
        System.out.println(file);
        System.out.println("--------");
        //构造器传入的路径
        System.out.println(file.getParent());
        //绝对的路径
        System.out.println(file.getAbsolutePath());
        //规范的路径
        try {
            System.out.println(file.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //两种类型的判断 判断文件是文件本身，还是文件夹
        System.out.println(file.isFile());
        System.out.println(file.isDirectory());

        File dir = new File(projectPath+"/"+"file");
        System.out.println(dir.isFile());
        System.out.println(dir.isDirectory());
        System.out.println("--------");

        //三种权限的判断 可读 可写 可执行
        System.out.println(file.canRead());
        System.out.println(file.canWrite());
        System.out.println(file.canExecute());

        //创建和删除文件
        //需要注意路径的正确性
        File newFile = new File(projectPath);
        System.out.println(newFile.isFile());
        System.out.println(newFile.isDirectory());
        try {
            Boolean result = newFile.createNewFile();
            if (result){
                System.out.println("创建文件成功");
                System.out.println(newFile);
            }else {
                System.out.println("文件创建失败");
                //重复执行则会创建失败,因为已经有同名文件了
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //判断文件是否存在
        System.out.println(newFile.exists());
        System.out.println(file.exists());
    }
}
